﻿using System;
using System.Xml.Linq;
using Engine;
using Game;

namespace SAGhoul
{
	public class OptionDialog : Dialog
	{
		public static int[] PeriodLengthPeriodLengthMultipliers { get; set; } = [1, 2, 3, 4];
		public ButtonWidget m_difficultyButton;
		public ButtonWidget m_periodLengthButton;
		public ButtonWidget m_okButton;
		public ButtonWidget m_cancelButton;
		public LabelWidget m_difficultyTip;
		public LabelWidget m_periodLengthTip;
		public SubsystemSAG m_subsystemSAG;

		int selectedDifficulty;
		int selectedPeriodLength;

		public OptionDialog(SubsystemSAG subsystemSAG)
		{
			m_subsystemSAG = subsystemSAG;
			XElement node = ContentManager.Get<XElement>("Dialogs/OptionDialog");
			LoadContents(this, node);
			m_difficultyButton = Children.Find<BevelledButtonWidget>("DifficultyButton");
			m_periodLengthButton = Children.Find<BevelledButtonWidget>("PeriodLengthButton");
			m_okButton = Children.Find<BevelledButtonWidget>("OKButton");
			m_cancelButton = Children.Find<BevelledButtonWidget>("CancelButton");
			m_difficultyTip = Children.Find<LabelWidget>("DifficultyTip");
			m_periodLengthTip = Children.Find<LabelWidget>("PeriodLengthTip");
			selectedDifficulty = (int)subsystemSAG.Difficulty;
			selectedPeriodLength = subsystemSAG.PeriodLengthMultiplier;
			RefreshDifficultyText();
			RefreshPeriodLengthText();
		}

		public override void Update()
		{
			if (m_difficultyButton.IsClicked)
			{
				selectedDifficulty = (selectedDifficulty + 1) % Enum.GetValues<Difficulty>().Length;
				RefreshDifficultyText();
			}
			if (m_periodLengthButton.IsClicked)
			{
				selectedPeriodLength = PeriodLengthPeriodLengthMultipliers[(GetPeriodLengthIndex(selectedPeriodLength) + 1) % PeriodLengthPeriodLengthMultipliers.Length];
				RefreshPeriodLengthText();
			}
			if (m_okButton.IsClicked)
			{
				m_subsystemSAG.IsOptionDecided = true;
				m_subsystemSAG.Difficulty = (Difficulty)selectedDifficulty;
				m_subsystemSAG.PeriodLengthMultiplier = selectedPeriodLength;
				m_subsystemSAG.DisplaySetMessage();
				DialogsManager.HideDialog(this);
			}
			if (Input.Cancel || m_cancelButton.IsClicked)
			{
				m_subsystemSAG.IsOptionDecided = true;
				m_subsystemSAG.DisplaySetMessage();
				DialogsManager.HideDialog(this);
			}
		}

		void RefreshDifficultyText()
		{
			m_difficultyButton.Text = LanguageControl.Get(typeof(SubsystemSAG).Name, "Difficulties", selectedDifficulty.ToString());
			m_difficultyTip.Text = LanguageControl.Get("ContentWidgets", typeof(OptionDialog).Name, "DifficultyTips", selectedDifficulty.ToString());
		}
		void RefreshPeriodLengthText()
		{
			m_periodLengthButton.Text = (selectedPeriodLength * 4).ToString() + LanguageControl.Get(typeof(SubsystemSAG).Name, "Day");
			m_periodLengthTip.Text = LanguageControl.Get("ContentWidgets", typeof(OptionDialog).Name, "PeriodLengthTips", GetPeriodLengthIndex(selectedPeriodLength).ToString());
		}
		static int GetPeriodLengthIndex(int periodLength) => MathUtils.Clamp(PeriodLengthPeriodLengthMultipliers.FirstIndex(periodLength), 0, PeriodLengthPeriodLengthMultipliers.Length - 1);
	}
}
